package com.blb.exam.dao.impl;

import com.blb.exam.common.entity.QuetionSet;
import com.blb.exam.common.util.DBUtils;
import com.blb.exam.dao.QuetionSetDAO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 考试题集DAO实现
 */
@Repository
public class QuetionSetDAOImpl implements QuetionSetDAO {

    @Override
    public List<QuetionSet> selectAll() {
        List<QuetionSet> quetionSets = DBUtils.executeQuery(QuetionSet.class, "select q.*,s.subject_name from t_quetion_set q join t_subject s on q.subject_id = s.id ");
        return quetionSets;
    }

    @Override
    public QuetionSet selectById(Long id) {
        List<QuetionSet> quetionSets = DBUtils.executeQuery(QuetionSet.class, "select q.*,s.subject_name from t_quetion_set q join t_subject s on q.subject_id = s.id where q.id = ?", id);
        if(quetionSets != null && !quetionSets.isEmpty()){
            return quetionSets.get(0);
        }
        return null;
    }

    @Override
    public int insert(QuetionSet quetionSet) {
        int rows = DBUtils.executeUpdate("insert into t_quetion_set(title,description,point,subject_id,time) values(?,?,?,?,?)",
                quetionSet.getTitle(),quetionSet.getDescription(),quetionSet.getPoint(),quetionSet.getSubjectId(),quetionSet.getTime());
        return rows;
    }

    @Override
    public int update(QuetionSet quetionSet) {
        int rows = DBUtils.executeUpdate("update t_quetion_set set title=?,description=?,point=?,subject_id=?,time=? where id=?",
                quetionSet.getTitle(), quetionSet.getDescription(), quetionSet.getPoint(), quetionSet.getSubjectId(), quetionSet.getTime(), quetionSet.getId());
        return rows;
    }

    @Override
    public int deleteById(int stuId) {
        int rows = DBUtils.executeUpdate("delete from t_quetion_set where id=?",stuId);
        return rows;
    }

    @Override
    public List<QuetionSet> selectLimitLikeTitle(String title, int start, int length) {
        return DBUtils.executeQuery(QuetionSet.class,"select q.*,s.subject_name from t_quetion_set q join t_subject s on q.subject_id = s.id where title like ? limit ?,?",
                "%" + title + "%",start,length);
    }

    @Override
    public int selectCountLikeTitle(String title) {
        return DBUtils.executeCount("select count(*) from t_quetion_set where title like ?","%" + title + "%");
    }

}
